package com.poi.exp.analysis;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.poi.exp.domain.BaseModel;
import com.poi.exp.domain.CellModelVO;
import com.poi.exp.domain.RowModelVO;
import com.poi.exp.domain.SingleRowModelVO;
import com.poi.util.PoiContextUtil;

/**
 * 默认的行处理器。单元格内容中含有${key}的进行替换，否则不处理
 * 
 * @author wzy
 */
public class DefaultRowAnalysis implements RowAnalysis {
	private static final Logger log = LoggerFactory.getLogger(DefaultRowAnalysis.class);

	public static class SINGLE {
		public static DefaultRowAnalysis INSTANCE = new DefaultRowAnalysis();
	}

	@Override
	public int support(Row row) {
		return 0;
	}

	@Override
	public RowModelVO dealRow(Row currentRow) {
		SingleRowModelVO rowVO = new SingleRowModelVO();
		List<BaseModel> cellList = new ArrayList<>(currentRow.getLastCellNum());
		for (int i = 0; i < currentRow.getLastCellNum(); i++) {
			Cell cell = currentRow.getCell(i);
			if (null != cell) {
				String cellContent = cell.getStringCellValue();
				CellModelVO cellVO = new CellModelVO();
				cellList.add(cellVO);
				cellVO.setCell(cell);
				cellVO.setExpression(cellContent);
				log.info("解析rowIndex={},columnIndex={}", cell.getRowIndex(), cell.getColumnIndex());
				// String resolvedContent = RegexUtils.resolve(cellContent);
				cell.setCellValue(cellContent);
			}
		}
		rowVO.setCellList(cellList);
		PoiContextUtil.getContext().setWriterRowNum(currentRow.getRowNum()+1);
		rowVO.setRow(currentRow);
		return rowVO;
	}
}
